package models

import (
	"github.com/beego/beego/v2/client/orm"
	"time"
)

type ContentList struct {
	Id int64 `orm:"pk;auto;column(id)"`
	CompanyName string `orm:"size(100);column(company_name)"`
	CompanyId int64 `orm:"type(int);column(company_id)"`
	CreateDate time.Time `orm:"type(date);column(create_date)"`
	UpdateDate time.Time `orm:"type(date);column(update_date)"`
	Delete int `orm:"type(int);column(delete)"`
	UserId int64 `orm:"type(int);column(user_id)"`
	UserName string `orm:"size(100);column(user_name)"`
	Desc string `orm:"size(255);column(desc)"`
	ImageUrl string `orm:"size(255);column(image_url)"`
	Title string `orm:"size(255);column(title)"`
}

type ContentDetail struct {
	Id int64 `orm:"pk;auto;column(id)"`
	CreateDate time.Time `orm:"type(date);column(create_date)"`
	UpdateDate time.Time `orm:"type(date);column(update_date)"`
	Delete int `orm:"type(int);column(delete)"`
	ImageUrl string `orm:"size(255);column(image_url)"`
	InterviewStep string `orm:"size(100);column(interview_step)"`
	InterviewType string `orm:"size(100);column(interview_type)"`
	Content string `orm:"size(1000);column(content)"`
	ContentListId int64 `orm:"type(int);column(content_list_id)"`
}


func (detail *ContentDetail) TableName()string  {
	return "t_content_detail"
}

func (list *ContentList) TableName()string  {
	return "t_content_list"
}

func QueryDetailById(contentId int64) ContentListVO  {
	var contentList ContentList
	newOrm := orm.NewOrm()
	newOrm.Raw("select * from t_content_list where id =?",contentId).QueryRow(&contentList)
	if contentList==(ContentList{}){
		return ContentListVO{}
	}
	var contentDetailList []ContentDetail
	newOrm.Raw("select * from t_content_detail where content_list_id =?",contentList.Id).QueryRows(&contentDetailList)
	return ContentListVO{ContentListKey: contentList,ContentDetailList: contentDetailList}
}

type ContentListVO struct {
	ContentListKey ContentList
	ContentDetailList []ContentDetail
}

//QueryList /**入参  currentPage currentPageCount keyParams
func QueryList(keyParams string,params ...int) []ContentList{
	currentPage := params[0]
	currentPageCount := params[1]
	startIndex := (currentPage - 1) * currentPageCount
	var resultList [] ContentList
	newOrm := orm.NewOrm()
	var sql = "select * from t_content_list where title like '%"+keyParams+"%' limit ?,?"
	_,err:=newOrm.Raw(sql, startIndex, currentPageCount).QueryRows(&resultList)
	if err!=nil{
		panic(err)
	}
	return resultList
}

func QueryCountByParams(params string) int {
	newOrm := orm.NewOrm()
	count :=0
	err := newOrm.Raw("select count(*) from t_content_list where title like '%" + params + "%'").QueryRow(&count)
	if err!=nil{
		panic(err)
	}
	return count
}

func AddContentList(list ContentList) bool{
	newOrm := orm.NewOrm()
	var sql = "insert into t_content_list (company_name,company_id,create_date,update_date,`delete`,user_id,user_name,`desc`,image_url,title) values (?,?,?,?,?,?,?,?,?,?)"
	_, err := newOrm.Raw(sql, list.CompanyName, list.CompanyId, list.CreateDate, list.UpdateDate, list.Delete, list.UserId, list.UserName, list.Desc, list.ImageUrl, list.Title).Exec()
	if err!=nil{
		panic(err)
		return false
	}
	return true
}
